package com.woniuxy.portal.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniuxy.portal.model.entity.China;
import com.woniuxy.portal.service.ChinaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 地址控制器
 */
@CrossOrigin(origins = "*")
@Api(tags = "地址接口")
@RestController
@RequestMapping("/china")
public class ChinaController {

    private final ChinaService chinaService;

    @Autowired
    public ChinaController(ChinaService chinaService) {
        this.chinaService = chinaService;
    }

    /**
     * 获取所有省份信息
     * 通过查询pid为0且id不为0的中国地区数据，按id升序排列，返回省份列表
     *
     * @return ResponseEntity<List < China>> 包含所有省份信息的响应实体，数据按id升序排列
     */
    @GetMapping("/getAllProvince")
    @ApiOperation("获取所有省份")
    public ResponseEntity<List<China>> getAllProvince() {
        // 查询所有省份数据：pid为0表示省级行政区，排除id为0的特殊记录，按id升序排列
        List<China> list = chinaService.list(
                Wrappers.lambdaQuery(China.class)
                        .eq(China::getPid, 0)
                        .ne(China::getId, 0).orderByAsc(China::getId)
        );
        return ResponseEntity.ok(list);
    }

}
